import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class MyQQLogin {
    public static void main(String[] args) {
        JFrame f = new JFrame("模拟QQ登录");
        f.setSize(600, 500);
        f.setLocation(300, 300);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setResizable(false); // 设置窗口不可拉伸

        JPanel panel = new JPanel();
        panel.setLayout(null); // 使用绝对定位
        panel.setBackground(Color.WHITE); // 设置背景颜色为白色
        f.add(panel);

        JPanel panel2 = new JPanel();
        JLabel label = new JLabel(new ImageIcon("images\\background.png"));
        panel2.add(label);
        panel2.setBounds(0, 0, 591, 216); // 使用setBounds方法设置组件的位置和大小
        panel2.setBackground(Color.WHITE); // 设置背景颜色为白色
        panel.add(panel2);

        JPanel panelCenter = new JPanel();
        panelCenter.setLayout(null);//绝对定位
        panelCenter.setBackground(Color.WHITE); // 设置背景颜色为白色
        /*----------------qq号码start--------------------*/
        JPanel QQNumber = new JPanel();
        JLabel labelQQ = new JLabel(new ImageIcon("images\\qq.png"));
        JTextField Number = new JTextField("QQ号码/手机/邮箱", 20);
        Number.setPreferredSize(new Dimension(400, 35));
        Number.setBorder(null);
        Number.setFont(new Font("宋体", Font.PLAIN, 16));
        QQNumber.add(labelQQ);
        QQNumber.add(Number);
        QQNumber.setBounds(-100, 0, 400, 40);
        QQNumber.setBackground(Color.WHITE); // 设置背景颜色为白色
        panelCenter.add(QQNumber);
        //对Number输入框进行监控
        // 添加焦点监听器
        Number.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
        Number.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                if (Number.getText().equals("QQ号码/手机/邮箱")) {
                    Number.setText("");
                    Number.setForeground(Color.BLACK);
                }
                QQNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
                Number.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
            }

            @Override
            public void focusLost(FocusEvent e) {
                if (Number.getText().isEmpty()) {
                    Number.setForeground(Color.GRAY);
                    Number.setText("QQ号码/手机/邮箱");
                }
                QQNumber.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
                Number.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
            }
        });
        /*---------------qq号码end--------------------*/
        /*---------------qq密码start------------------*/
        JPanel passWord = new JPanel();
        JLabel labelPassWord = new JLabel(new ImageIcon("images\\password.png"));
        JTextField pass = new JTextField("密码", 20);
        pass.setPreferredSize(new Dimension(400, 35));
        pass.setBorder(null);
        pass.setFont(new Font("宋体", Font.PLAIN, 16));
        pass.setForeground(Color.GRAY);
        passWord.add(labelPassWord);
        passWord.add(pass);
        passWord.setBounds(-100, 40, 400, 40);
        passWord.setBackground(Color.WHITE); // 设置背景颜色为白色
        panelCenter.add(passWord);
        passWord.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
        pass.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
        pass.addFocusListener(new FocusListener() {
            @Override
            public void focusGained(FocusEvent e) {
                String password = new String(pass.getText());
                if (password.equals("密码")) {
                    pass.setText("");
                    pass.setForeground(Color.BLACK);
                }
                passWord.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
                pass.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.BLUE));
            }

            @Override
            public void focusLost(FocusEvent e) {
                String password = new String(pass.getText());
                if (password.isEmpty()) {
                    pass.setForeground(Color.GRAY);
                    pass.setText("密码");
                }
                passWord.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
                pass.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, new Color(192, 192, 192)));
            }
        });
        /*---------------qq密码end------------------*/


        /*---------------多选按钮start--------------*/
        JPanel choice = new JPanel();
        choice.setLayout(null);
        JCheckBox automaticLogin = new JCheckBox("自动登录");
        automaticLogin.setBackground(Color.WHITE);
        automaticLogin.setBounds(0, 0, 100, 40);

        JCheckBox rememberPass = new JCheckBox("记住密码");
        rememberPass.setBounds(125, 0, 100, 40);

        JLabel findPass = new JLabel("找回密码");
        findPass.setBounds(250, 0, 100, 40);

        automaticLogin.setOpaque(false); // 设置为不透明
        rememberPass.setOpaque(false); // 设置为不透明
        findPass.setForeground(Color.black); // 设置字体颜色为蓝色
        choice.add(automaticLogin);
        choice.add(rememberPass);
        choice.add(findPass);
        choice.setBounds(0, 80, 400, 40);
        choice.setBackground(Color.WHITE); // 设置背景颜色为白色
        panelCenter.add(choice);

        /*------------多选按钮end------------*/

        /*-------------登录界面start-------------*/
        JPanel login = new JPanel();
        JButton secureLogin = new JButton("secureLogin", new ImageIcon("images\\安全登录.png"));
        secureLogin.setPreferredSize(new Dimension(320, 40));
        secureLogin.setBackground(new Color(8, 186, 253));
        secureLogin.setFont(new Font("Arial", Font.BOLD, 16)); // 设置字体为Arial，加粗，大小为16
        secureLogin.setForeground(Color.WHITE); // 设置字体颜色为白色
        secureLogin.setBorderPainted(false); // 去除按钮的边框绘制
        login.add(secureLogin);
        login.setBounds(0, 120, 320, 50);
        login.setBackground(new Color(8, 186,253));
        panelCenter.add(login);
        secureLogin.addActionListener(new ActionListener() {//事件的监控
            @Override
            public void actionPerformed(ActionEvent e) {
                String account = Number.getText();
                String password = pass.getText();
                if (account.equals("admin") && password.equals("admin")) {
                    JOptionPane.showMessageDialog(f, "登录成功");
                } else if(account.equals("QQ号码/手机/邮箱")) {
                    JOptionPane.showMessageDialog(f, "请输入账号");
                } else if(password.equals("密码")) {
                    JOptionPane.showMessageDialog(f, "请输入密码");
                }
                else {
                    JOptionPane.showMessageDialog(f, "账号或密码错误");
                }
            }
        });
        /*------------登录界面end----------------------*/

        JPanel last = new JPanel();
        last.setBounds(20, 420, 60, 30);
        last.setBackground(Color.WHITE); // 设置背景颜色为白色
        JPanel register = new JPanel();//注册账号
        register.setBackground(Color.WHITE);
        JLabel signIn = new JLabel("注册账号");
        signIn.setFont(new Font("宋体", Font.PLAIN, 14));// 设置字体
        signIn.setForeground(Color.GRAY);// 设置字体颜色
        register.add(signIn);
        last.add(register);


        JPanel QRCode = new JPanel();
        QRCode.setBackground(Color.WHITE);
        QRCode.setBounds(500, 400, 45, 46);
        JLabel QR = new JLabel(new ImageIcon("images\\QRCode.png"));
        QRCode.add(QR);

        panelCenter.setBounds(130, 240, 340, 180);//居中显示
        panel.add(panelCenter); // 将panelCenter添加到panel中
        panel.add(last);
        panel.add(QRCode);

        f.setVisible(true);
        f.addWindowListener(new WindowAdapter() {
            public void windowOpened(WindowEvent e){
                f.requestFocus(); // 将焦点设置到窗体上
            }
        });

    }
}
